package com.hcrazy.manchester.app.controllers.member;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hcrazy.manchester.app.pojo.LinkUs;
import com.hcrazy.manchester.app.services.LinkUsService;
import com.hcrazy.manchester.app.utils.Constant;
import com.hcrazy.manchester.app.utils.Log;

@Controller
@RequestMapping("/admin/more")
public class LinkusController {
	
	@Autowired
	private LinkUsService linkUsService;
	
	
	/**
	 * 跳转新增页面
	 * 初始化页面数据
	 * @param model
	 */
	@RequestMapping(value="/linkUs",method=RequestMethod.GET)
	public void linkUs(Model model){
		try {
			LinkUs linkUs = linkUsService.getLinkUs(Constant.MORE_LINK_US_ID);
			model.addAttribute("info", linkUs);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
		}
	}
	
	@RequestMapping(value="/declare",method=RequestMethod.GET)
	public void declare(Model model){
		try {
			LinkUs linkUs = linkUsService.getLinkUs(Constant.MORE_DECLARE_ID);
			model.addAttribute("info", linkUs);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
		}
	}
	
	/**
	 * 提交信息内容
	 * @param food
	 * @return
	 */
	@RequestMapping(value="/linkUs",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_VALUE)
    public String submitMessage(@ModelAttribute("form") LinkUs info,Model model){
		try {
			linkUsService.updateLinkUs(info);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			model.addAttribute("message", e.getMessage());
			return "/admin/failed";
		}
		return "redirect:/admin/more/linkUs";
    }
	
	@RequestMapping(value="/declare",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_VALUE)
    public String declare(@ModelAttribute("form") LinkUs info,Model model){
		try {
			linkUsService.updateLinkUs(info);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			model.addAttribute("message", e.getMessage());
			return "/admin/failed";
		}
		return "redirect:/admin/more/declare";
    }
	
	
}
